<?php
/**
 * 字典库
 * User: lxl
 * Date: 2020/07/22
 */

namespace App\Service\Core\Sys\Dict;

use App\Model\Core\Sys\SysDictModel;
use App\Service\ParamsService;
use App\Service\Core\CoreBaseService;
use App\Traits\SingleInstanceTrait;

class SysDictService extends CoreBaseService
{
    use SingleInstanceTrait;

    /**
     * 获取字典信息
     * @param int $id
     * @return array
     */
    public function getOne(int $id): array
    {
        return SysDictModel::getInstance()->getOne($id);
    }

    /**
     * 获取指定分类下全部字典
     * @param $item
     * @return array
     */
    public function getDictAll($item): array
    {
        $list = [];
        $m_list = SysDictModel::getInstance()->getAllByItem($item, ParamsService::STATUS_ENABLE);
        foreach ($m_list as $item) {
            $list[] = [
                'dict_item' => $item['dict_item'],
                'dict_sub_val' => $item['dict_sub_val'],
                'dict_sub_val_ext' => $item['dict_sub_val_ext'],
            ];
        }
        return $list;
    }

    /**
     * 获取子分类数据字典
     * @param $f_item
     * @param $s_item
     * @return array
     */
    public function getSubDic($f_item, $s_item): array
    {
        $m_list = SysDictModel::getInstance()->getAllByItem($f_item, ParamsService::STATUS_ENABLE);
        foreach ($m_list as $item) {
            if ($s_item == $item['dict_sub_val']) {
                return $item;
            }
        }
        return [];
    }

    /**
     * 获取字典项数据
     * @return array
     */
    public function getItemTreeList(): array
    {
        $res = [];
        foreach (ParamsService::$dict_item_list as $item => $value) {
            $res[] = [
                'item' => $item,
                'value' => $value,
            ];
        }
        return $res;
    }

}
